//
//  WOHistoryEntryDetailViewController.m
//  WOPrototype
//
//  Created by Olof Millberg on 2010-04-21.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOHistoryEntryDetailViewController.h"

#import "WOTextSection.h"
#import "WOContactSection.h"
#import "WOPrototypeAppDelegate.h"

#import "TextCell.h"
#import "ContactCell.h"

@implementation WOHistoryEntryDetailViewController

#pragma mark Initialization Methods

- (id) initWithWorkOrder:(WorkOrder *)aWorkOrder historyEntry:(WOHistoryEntry *)aHistoryEntry
{
	if (self = [super initWithStyle:UITableViewStyleGrouped])
	{
		self.workOrder = aWorkOrder;
		historyEntry = [aHistoryEntry retain];
	}
	
	return self;
}

- (void) loadView
{
	[super loadView];
	
	self.sections = [NSMutableArray array];
	
	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateFormat:@"EEEE d MMMM YYYY"];
	[self.sections addObject:[WOTextSection textSectionWithTitle:[dateFormatter stringFromDate:historyEntry.entryDate]
																		  text:historyEntry.description]];
	
	UILabel *titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)] autorelease];
	[titleView setBackgroundColor:[UIColor clearColor]];
	[titleView setTextColor:[UIColor whiteColor]];
	[titleView setTextAlignment:UITextAlignmentCenter];
	[titleView setText:self.title];
	self.navigationItem.titleView = titleView;
	
	if (historyEntry.author)
	{
		// TODO: Anpassa titel efter typ på ett snyggare sätt
		NSString *title = ([historyEntry.entryType isEqualToString:@"Note"]) ? @"Skrivet av" : @"Arbetsledare";
		[self.sections addObject:[WOContactSection contactSectionWithTitle:title 
															   contactList:[NSMutableArray arrayWithObject:historyEntry.author] 
																  editable:NO]];
	}
}

- (void) dealloc
{
	[historyEntry release];
	[super dealloc];
}

#pragma mark -

#pragma mark UITableView Methods

/* Sets the cell for each row */
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell* cell = [super tableView:aTableView cellForRowAtIndexPath:indexPath];
	
	if ([cell isKindOfClass:[TextCell class]])
	{
		((TextCell *)cell).text = historyEntry.description;
		WOPrototypeAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
		if ([historyEntry.author.socialID isEqualToString:appDelegate.currentUser.socialID])
		{
			((TextCell *)cell).editable = YES;
			((TextCell *)cell).delegate = self;
			((TextCell *)cell).title = @"Redigera notis";
			((TextCell *)cell).viewController = self;
		}
									 
	}
	else if ([cell isKindOfClass:[ContactCell class]])
	{
		((ContactCell *)cell).contactPerson = historyEntry.author;
		((ContactCell *)cell).viewController = self;
	}
	return cell;
}

- (CGFloat) tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	WOTableSection *section = [self.sections objectAtIndex:indexPath.section];
	if ([section isKindOfClass:[WOTextSection class]])
		((WOTextSection*)section).text = historyEntry.description;
	return [super tableView:aTableView heightForRowAtIndexPath:indexPath];
}

- (BOOL) noteView:(WONewNoteViewController *)aNoteView shouldReturnWithText:(NSString *)text
{
	historyEntry.description = text;
	
	if (![self.workOrder.updatedHistoryEntries containsObject:historyEntry])
		[self.workOrder.updatedHistoryEntries addObject:historyEntry];
	
	[self.tableView reloadData];
	return YES;
}
@end